<template>
	<view>
		<!-- 头部搜索 -->
		<view class="top-search">
			<!-- <view class="arrow-left">
				<u-icon size="30" name="arrow-left"></u-icon>
			</view> -->
			<view class="search">
				<u-search v-model="foodName" :show-action="true" action-text="搜索" :clearabled="true" :animation="true"></u-search>
				<view class="search-list" v-if="detailList.length != 0">
					<u-list preLoadScreen="4">
					<view class="" v-for="item in detailList" :key="item.id" style="height: 60rpx;">
						<navigator open-type="navigate" 
								:url="'/subpages/foodDetail/foodDetail?id=' + item.id + '&foodName=' + item.foodName + '&imgUrl=' + item.imgUrl">
						<view style="margin-top: 10rpx;margin-bottom: 10rpx;">
							{{item.foodName}}
						</view>
						<u-line color="#cccccc" />
						</navigator>
					</view>
					<view style="height: 100rpx;">
						
					</view>
					</u-list>
				</view>
				
			</view>

		</view>
		<u-line color="#cccccc" />
		<!-- 主体内容 -->
		<view class="main">
			<u-row customStyle="margin-bottom: 10px;align-items: start;">
				<!-- 左边部分 -->
				<u-col span="3">
					<view class="bg-grey">
						<u-list>
							<view v-for="item in foodTypeList" :key="item.id" :class="{list_item: item.id == foodTypeId}">
								<u-list-item>
									<u-cell :title="item.typeName" @click="foodDetailType(item.id,item.typeName)">
									</u-cell>
								</u-list-item>
							</view>
						</u-list>
					</view>
				</u-col>
				<!-- 右边部分 -->
				<u-col span="9">
					<view>
						<text style="font-size: 40rpx;font-weight: bold;margin: 10rpx;">{{typeName}}</text>
						<!-- <view>
							<view v-for="item in foodDetailTypeList" :key="item.id" class="detail-item">
								{{item.typeName}}
							</view>
						</view> -->
						<u-list>
							<view v-for="item in foodDetailTypeList" :key="item.id">
								<navigator open-type="navigate" 
								:url="'/subpages/foodDetailList/foodDetailList?id=' + item.id + '&typeName=' + item.typeName">
								<u-list-item>
									<view class="detail-item1">
										<text style="width: 50%;">{{item.typeName}}</text>
									<u-icon size="20" name="arrow-right"></u-icon>
									</view>
									<u-line color="#cccccc" />
								</u-list-item>
								</navigator>
							</view>
						</u-list>
					</view>
				</u-col>
			</u-row>
		</view>
	</view>
</template>

<script>
	import {
		foodTypeList,
		foodDetailTypeByFoodTypeIdList,
		foodDetailByTypeIdList,
		foodDetailByFoodNameList
	} from '@/config/api.js'
	export default {
		data() {
			return {
				foodTypeList: [],
				foodDetailTypeList: [],
				foodTypeId:"",
				typeName: "",
				foodName:"",
				detailList: [],
			};
		},
		watch:{
			foodName(newVal,oleVal){
				if(newVal == '' || newVal == null){
					this.detailList = []
				} else {
					this.LoadFoodDetailByFoodName(newVal);
				}
			}
		},
		onLoad() {
			this.LoadFoodType();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.LoadFoodType();
			// 加载完成后关闭加载动画
			uni.stopPullDownRefresh();
		},
		methods: {
			// 加载一级分类列表
			async LoadFoodType() {
				const data = await foodTypeList();
				if (data.code == 200) {
					this.foodTypeList = data.rows;
				}
				this.foodTypeId = this.foodTypeList[0].id;
				this.typeName = this.foodTypeList[0].typeName;
				this.isSelect = true;
				this.foodDetailType(this.foodTypeId,this.typeName);
			},
			// 根据一级分类id加载二级分类列表
			async LoadFoodDetailType(foodTypeId) {
				const data = await foodDetailTypeByFoodTypeIdList(foodTypeId);
				if (data.code == 200) {
					this.foodDetailTypeList = data.rows;
				}
			},
			// // 根据二级分类id查询食物详情列表
			// async LoadFoodDetail(typeId){
			// 	const data = await foodDetailByTypeIdList(typeId);
			// 	if (data.code == 200) {
			// 		console.log(data.rows);
			// 	}
			// },
			// 根据食物名模糊查询食物列表
			async LoadFoodDetailByFoodName(foodName){
				const data = await foodDetailByFoodNameList(foodName);
				if (data.code == 200) {
					this.detailList = data.data;
				}
			},
			// 一级分类点击项
			foodDetailType(foodTypeId, typeName) {
				this.LoadFoodDetailType(foodTypeId);
				this.foodTypeId = foodTypeId;
				this.typeName = typeName;
			},
			// // 二级分类点击项
			// foodDetail(typeId,typeName){
			// 	this.LoadFoodDetail(typeId);
			// }

		}
	}
</script>

<style lang="scss" scoped>
	.top-search {
		position: fixed;
		width: 100%;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		background-color: #ffffff;
		z-index: 10;

		.arrow-left {
			margin-right: 20rpx;
		}

		.search {
			width: 80%;
		}
	}

	.main {
		width: 100%;
		margin-top: 80rpx;

		.bg-grey {
			background-color: #f8f8f8;
		}

		
		.detail-item {
			display: inline-block;
			padding: 10rpx 30rpx;
			border-radius: 20rpx;
			border: #007AFF solid 1px;
			margin: 10rpx 20rpx 10rpx 20rpx;
		}
		.detail-item1 {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			font-size: 35rpx;
			font-weight: 400;
			margin: 20rpx 10rpx 20rpx 10rpx;
		}
		
	}
	.list_item{
		background-color: #63b7af;
		color: #ffffff;
	}
</style>
